// my/myConsult/myConsult.js
Page({
  data: {
    orders: [],
    searchKeyword: '',
    loading: false
  },

  onLoad() {
    // 页面加载时获取所有订单
    this.fetchOrders();
  },

  // 输入搜索关键词
  onSearchInput(e) {
    this.setData({
      searchKeyword: e.detail.value
    });
  },

  // 搜索订单
  searchOrders() {
    this.fetchOrders();
  },

  // 获取订单数据
  fetchOrders() {
    const { searchKeyword } = this.data;
    this.setData({ loading: true });

    wx.request({
      url: 'http://localhost:8080/consultantOrder/condition',
      method: 'GET',
      data: {
        consultantName: searchKeyword || ''
      },
      success: (res) => {
        if (res.statusCode === 200 && res.data.code === 200) {
          this.setData({
            orders: res.data.data || []
          });
        } else {
          wx.showToast({
            title: '获取订单失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      },
      complete: () => {
        this.setData({ loading: false });
      }
    });
  },

  // 跳转到评价页面
  navigateToEvaluate(e) {
    const consultantId = e.currentTarget.dataset.consultantId;
    wx.navigateTo({
      url: '/my/evaluate/evaluate?consultantId=' + consultantId
    });
  }
});